#include "threadPool.h"
#include "head.h"

int makeWorker(threadPool_t *threadPool){
    for(int i = 0; i < threadPool->workerNum; ++i){
        pthread_create(&threadPool->tidArr[i],NULL,threadFunc,threadPool);
    }
    return 0;
}

void *threadFunc(void *arg){
    threadPool_t *threadPool = (threadPool_t *)arg;

    MYSQL* mysql;//访问数据库
    while(1){
        // 取出任务
        pthread_mutex_lock(&threadPool->taskQueue.mutex);
        while(threadPool->exitflag == 0 && threadPool->taskQueue.queueSize == 0){
            pthread_cond_wait(&threadPool->taskQueue.cond, &threadPool->taskQueue.mutex);
        }
        if(threadPool->exitflag != 0){
            printf("child is going to exit!\n");
            pthread_mutex_unlock(&threadPool->taskQueue.mutex);
            pthread_exit(NULL);
        }
        printf("child get a task!\n");
        int netfd = threadPool->taskQueue.pFront->netfd;
        deQueue(&threadPool->taskQueue);
        init_Mysql(&mysql);
        pthread_mutex_unlock(&threadPool->taskQueue.mutex);
        // 执行任务
        int reg_log_flag;
        int file_system_id;
        recv(netfd,&reg_log_flag,sizeof(int),MSG_WAITALL);
        if(reg_log_flag ==1)
        {
            while(1)
            {
                int ret = user_Register(netfd,mysql); 
                if(ret != -1)
                {
                    file_system_id = ret;
                    break;
                }
            }
        }
        else
        {
            while(1)
            {
                int ret = checkLogin(netfd,mysql);
                if(ret != -1)
                {
                    file_system_id = ret;
                    break;
                }
            }
        }

        while(1)
        {
            int orderflag;
            char path[256] = {0};
            recvOrder(netfd,&orderflag,path);
            printf("child path = %s\n",path);
            switch(orderflag)
            {
            case CD:
                {
                    int cd = myCd(netfd,file_system_id,path,mysql);
                    if(cd == 0)
                    {
                        printf("cd failed\n");
                    }
                    else
                    {
                        file_system_id = cd;
                        printf("cd success!\n");
                    }
                    break;
                }
            case LS:

                sendLs(netfd,file_system_id,mysql);
                printf("someone in ls\n");
                break;
            case PUTS:

                //putsSend(netfd,path);


            case GETS:
                printf("server has passed the switch\n");
                break;
            case PWD:
                sendPwd(netfd,file_system_id,mysql);
                break;
            case MKDIR:
                createMkdir(file_system_id,path,mysql);
                break;
            case REMOVE:
                {int ret=removefile(file_system_id,path,mysql);
                    if(ret==-1)
                    {
                        send(netfd,&ret,sizeof(int),MSG_NOSIGNAL);
                    }else
                    {
                        send(netfd,&ret,sizeof(int),MSG_NOSIGNAL);
                    }
                    break;
            
                }
        
            }
        }
        close(netfd);
        // 恢复空闲
        printf("child finish a task!\n");
    }
}


